package com.suyun.diyan;

import sun.misc.IOUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class CreateHiveFileMain {
    public static Map<String,String> historyDataMap = new HashMap<>();
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    public static void main(String[] args) {

        System.out.println("===========diyan start===========");
//        String inputFliePath = "data/source.txt";
//        String outputFliePath = "data/final.txt";
//        String historyFilePath = "data/history.txt";

        String inputFliePath = args[0];
        String outputFliePath = args[1];
        String historyFilePath = args[2];

        System.out.println("Month file path=" + inputFliePath);
        System.out.println("History file path=" + historyFilePath);
        System.out.println("Export file path=" + outputFliePath);

        getHistoryDataFromTxt(historyFilePath);

        System.out.println("historyMap size="+historyDataMap.size());

        createDiyanFile(inputFliePath,outputFliePath);
        System.out.println("===========diyan end===========");

    }


    private static Map<String, String> getHistoryDataFromTxt(String historyFilePath) {
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(historyFilePath), "UTF-8"));//构造一个BufferedReader类来读取文件
            String line = null;
            Calendar maxCal = Calendar.getInstance();
            while((line = br.readLine())!=null){
                String[] splits = line.split("\\|");
                String qq_no = splits[0];
                String platform = splits[1];
                String maxDate = splits[2];
                maxCal.setTime(sdf.parse(maxDate));
                historyDataMap.put(qq_no + "_" + platform, sdf.format(maxCal.getTime()));
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return historyDataMap;
    }


    private static void  createDiyanFile(String inputFliePath,String outputFliePath){
        try{
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(inputFliePath), "UTF-8"));//构造一个BufferedReader类来读取文件
            String line = null;
            Calendar diyanCal = Calendar.getInstance();

            FileOutputStream fos=new FileOutputStream(new File(outputFliePath));
            OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8");
            BufferedWriter  bw=new BufferedWriter(osw);

            while((line = br.readLine())!=null){
                MonthDiyan diyan = new MonthDiyan();
                String[] splits = line.split("\\|");
                String orderid = splits[0];
                String qq_no = splits[1];
                String platform = splits[2];
                String appid = splits[3];
                String areaid = splits[4];
                String data_desc = splits[5];
                String sign_date = splits[6];
                Integer open_days = Integer.valueOf(Integer.parseInt(splits[7]));
                Double amount = Double.valueOf(Integer.parseInt(splits[8]));


                diyan.setOrderId(orderid);
                diyan.setQqNo(qq_no);
                diyan.setPlatform(platform);
                diyan.setAppId(appid);
                diyan.setAreaId(areaid);
                diyan.setDataDesc(data_desc);
                diyan.setSignDate(sign_date);
                diyan.setOpenDays(open_days);
                diyan.setAmount(amount);
                diyan.setDayPrice(amount/open_days);

                String key = qq_no + "_" + platform;

                if(historyDataMap.containsKey(key) && Integer.parseInt(sign_date)< Integer.parseInt(historyDataMap.get(key))){
                    diyanCal.setTime(sdf.parse(historyDataMap.get(key)));
                    diyanCal.add(Calendar.DAY_OF_MONTH,1);
                    diyan.setStartDate(sdf.format(diyanCal.getTime()));
                }else{
                    diyanCal.setTime(sdf.parse(diyan.getSignDate()));
                    diyan.setStartDate(diyan.getSignDate());
                }
                diyanCal.add(Calendar.DAY_OF_MONTH,open_days);
                diyan.setEndDate(sdf.format(diyanCal.getTime()));
                bw.write(diyan.getHiveText());
            }


            br.close();
            bw.close();
            osw.close();
            fos.close();
        }catch(Exception e){
            e.printStackTrace();
        }



    }
}
